<jsp:useBean id="coord" class="dasregistry.coordBean" scope="page"/>
<jsp:directive.page import="java.util.*" />
<jsp:directive.page import="java.io.*" />
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasCoordinateSystem" />
<jsp:directive.page	import="org.biojava.dasobert.dasregistry.DasCoordSysComparator" />
<jsp:setProperty name="coord" property="*"/>
<jsp:directive.page import="org.biojava.dasobert.das.DasSpec" />
<jsp:directive.page import="org.biojava.services.das.registry.DasRegistrySql" />
<%@ include file="sangertablestart.jsp" %>

<tr class="row1">
	<td><a href="help_coordsys.jsp#organism">organism</a>:</td>
	<td><a href="help_coordsys.jsp#name">authority</a>:</td>
	<td><a href="help_coordsys.jsp#type">type</a>:</td>
	<td>&nbsp;</td>
</tr>
<form method="get" action="listServices.jsp" name="SBform" id="listForm">
<script>
var dol = new DynamicOptionList();

var coordAuth = new DynamicOptionList();
coordAuth.addDependentFields("organism","CSName","CSTypes");

dol.setFormName("SBform");
coordAuth.forValue("any").addOptions("any");
coordAuth.forValue("any").forValue("any").addOptions("any");

<%
String[] coordsNames = registry.getCoordSysTypes();
if ( coordsNames == null) 
	coordsNames = new String[0];

for (int i=0;i<coordsNames.length;i++){
	%>coordAuth.forValue("any").forValue("any").addOptions("<%=coordsNames[i]%>");
<%		
}

List<DasCoordinateSystem> coords = register.getAllCoordinateSystems();
//Comparator dscomp               = DasCoordSysComparator.fromString("name");
//java.util.Arrays.sort(coords,dscomp);

List orgs = new ArrayList();

for ( int i=0; i< coords.size();i++) {
	
	DasCoordinateSystem cs = coords.get(i);
	
	String authority = cs.getName();
	
	if ( authority.equals("") )
		authority = "any";
	
	if ( (cs.getVersion() != null) && 
			(! cs.getVersion().equals("")) ) {
		authority += "_" + cs.getVersion();
	}

	Map om = new HashMap();
	
	
	// authority -> type
	Map at = new HashMap();
	at.put(authority,cs.getCategory());
	
	String organism = cs.getOrganismName();
	
	if ( organism.equals("")){
		organism = "any";
	}
	
	om.put("organism",organism);
	
	List taxis = (List)om.get("list");
	if ( taxis == null )
		taxis = new ArrayList();

	taxis.add(at);
	// check if akred
	
	
	om.put("list",taxis);
	
	orgs.add(om);	
}

String defaultOrg = null ;
if ( (findAction != null) && ( findAction.equals("find"))) {
	defaultOrg = register.getOrganism();
}

Collections.sort(orgs, new Comparator() {
    public int compare(Object a, Object b) {
        Map x = (Map) a;
        Map y = (Map) b;
        String s = x.get("organism").toString();
        String t = y.get("organism").toString();
        if (s.equals("any")) {
            return -1;
        } else if (t.equals("any")) {
            return 1;
        }
        return s.compareTo( t );
    }
});
Iterator iter = orgs.iterator();
StringBuffer buf  = new StringBuffer();
StringBuffer buf3 = new StringBuffer();

List knownOptions= new ArrayList();
while (iter.hasNext()){
	
	Map orgM = (Map) iter.next();

	String orgl = (String) orgM.get("organism");
	
	List csses = (List) orgM.get("list");	

	Iterator iter2 = csses.iterator();			
	if ( ! orgl.equals("any") ) {
	%>coordAuth.forValue("<%=orgl%>").addOptions("any");
coordAuth.forValue("<%=orgl%>").forValue("any").addOptions("any");
<%}%>coordAuth.forValue("<%=orgl%>").addOptions(<%
	
	
	if ( ! knownOptions.contains(orgl) ) {			
		buf.append("<option value=\""+ orgl +"\"");
		
		if ( defaultOrg != null  ) {
			if ( defaultOrg.equals(orgl))
				buf.append(" selected ");
		} else {
			if (orgl.equals("any"))
				buf.append(" selected ");
		}
		
		buf.append(">"+orgl+"</option>");			
		knownOptions.add(orgl);
	}
	List knownAuth = new ArrayList();
	
	while (iter2.hasNext()) {
	
		Map m = (Map) iter2.next();
		Iterator iter3 = m.keySet().iterator();

		while (iter3.hasNext()){
			String authority = (String) iter3.next();		

			if ( ! knownAuth.contains(authority) ) {
			
				if (knownAuth.size() > 0)
					%>,<%
				knownAuth.add(authority);
				%>"<%=authority%>"<%	
				buf3.append("coordAuth.forValue(\""+orgl+"\").forValue(\""+authority+"\").addOptions(");			
				buf3.append("\""+m.get(authority)+"\"");
				buf3.append(");\n");
				buf3.append("coordAuth.forValue(\""+orgl+"\").forValue(\"any\").addOptions(");
				buf3.append("\""+m.get(authority)+"\"");
				buf3.append(");\n");
			}

		}
	}%>);
<%
}

out.print(buf3);

boolean defaultAuth = false;


String cSName   = register.getCSName();
String category = request.getParameter("CSTypes");
if ( (findAction != null ) && (findAction.equals("find")) ){
	if (  (category != null ) &&(!category.equals(""))){
	%>coordAuth.forValue("any").forValue("any").setDefaultOptions("<%=category%>");
	<%
	}
}
if ( (cSName != null ) && !(cSName.equals(""))){
	if ( (defaultOrg != null ) && !( defaultOrg.equals("") )) {
	%>coordAuth.forValue("<%=defaultOrg%>").setDefaultOptions("<%=cSName%>");
	<%
	defaultAuth = true;

	}
}
if ( ! defaultAuth) {
	if ( (defaultOrg != null ) && !( defaultOrg.equals("") )) {
		%>coordAuth.forValue("<%=defaultOrg%>").setDefaultOptions("any");
		<%
	} else {
		%>coordAuth.forValue("any").setDefaultOptions("any");
		<%
	}
}



%>

</script>

<tr class="row0"><td><select name="organism" id="organism"><%=buf %></select></td>
<td><select name="CSName" id=CSName><script language="javascript">coordAuth.printOptions("CSName")</script></select></td>
<td><select name="CSTypes" id=CSTypes><script language="javascript">coordAuth.printOptions("CSTypes")</script></select></td>
<td>&nbsp;</td>
</tr>

<tr class="row1">
	<td><a href="help_capabilities.jsp">capability</a>:</td>
	<td><a href="help_label.jsp">label</a>:</td>
	<td>Spec</a>:</td>
	<td>&nbsp;</td>
</tr>

<tr class="row0">

<!-- capabilities -->		
	<td>
    
      <select NAME="capabilities" size="1" id="capabilities">
      <option <%
       if (
    ( findAction == null ) ||
    (findAction.equals("list")))
     {
     out.print("selected");
     }
      %>value="" > any </option>

 <% 
 	  // all possible  capabilities
	  String all_capabilities[] = registry.getAllCapabilities();

	  // the selected capabilities
	  String[] capabilities = register.getCapabilities();
		   
	  String cap0 = "" ;

	  //System.out.println(capabilities.length);
	  if ( capabilities.length == 0 )  { 
	        out.print("setting caps %");
	  	capabilities = new String[1];
	  	capabilities[0] = "%";  
	  };
	  
	  if ( capabilities.length >  0 )  { cap0 = capabilities[0]; } 

	  int allcapabilitieslength = 0;
	  if ( all_capabilities != null ) {
	     allcapabilitieslength = all_capabilities.length ;
	  }

	  for (int i=0;i<allcapabilitieslength;i++){ 
	    out.print("<option ") ; 
	    if ( cap0.equals(all_capabilities[i])){
	     if ( ( findAction != null) && (! findAction.equals("list")))
		     {			     
	    	       out.print(" selected ");
		       }		  
		       }
	    out.println(" VALUE=\""+all_capabilities[i]+ "\">"+all_capabilities[i]+"</OPTION>") ;
		  
	}	
%>
		
      </select>
  	


	</td>


<!-- labels -->
       
	<td>
	<select name="labels" id="labels" size="1">

	<%
	String[] labels = registry.getAllLabels();
	String[]  curlabels = register.getLabels();
	%>
	<option value="any" <% 
	if ( curlabels.length == 0 ) { out.print("selected");}
	%>
	>any</option>
	<%
	
	int labelslength = 0;
	if ( labels != null ) {
	   labelslength = labels.length;
	}

	for ( int l = 0 ; l< labelslength;l++){
		String label = labels[l];
		%>
		<option value="<%=label%>" <%
		for ( int k=0; k< curlabels.length;k++) {
			if (curlabels[k].equals(label)){
			%>
			selected
			<%
			}
		}
		%>><%=label%></option>
		<%
	}
	%>
	</select>
	</td>
	<td><select name="spec" id="spec">
	<%
	DasSpec[] allSpecs = DasSpec.values();
	
	%>
	<option value="any" <% 
	if ( register.getSpec().equals("any")|| register.getSpec().equals("") ) { out.print("selected");}
	%>
	>any</option>
	<%

	for ( int l = 0 ; l< allSpecs.length;l++){
		String specOption = allSpecs[l].toString();
		%>
		<option value="<%=specOption%>" <%
		
			if (register.getSpec().equals(specOption)){
			%>
			selected
			<%
			}
		
		%>><%=specOption%></option>
		<%
	}
	%>
	</select>
	
	
	
	</td>

<td align="right">
<input type="hidden" name="cmd" value="find">
<%-- <input type="button" value="reset form" onClick="this.form.reset(); resetDynamicOptionLists(this.form);">--%>
<button  id="reset">Reset</button><input type="submit" value="display">
</td>
</tr>
</form>




<%@ include file="sangertableend.jsp" %>



 <!-- end of search menu -->